package com.itheima.tlias84.controller;

import com.itheima.tlias84.annotation.Log;
import com.itheima.tlias84.dto.SelectStudentDetailDTO;
import com.itheima.tlias84.po.InsertStudentParam;
import com.itheima.tlias84.entity.Student;
import com.itheima.tlias84.po.SelectStudentPageParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.po.UpdateVioStudentParam;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 学生控制器
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    StudentService service;

    /**
     * 查询所有学生
     * @return 结果
     */
    @GetMapping("/list")
    public ResultUtil selectStudentAll() {
        List<Student> list = service.selectStudentAll();
        return ResultUtil.success(list);
    }

    /**
     * 分页查询学生
     * @param param 查询参数
     * @return 结果
     */
    @GetMapping
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        //参数校验：在controller层中实现-->page、pageSize不能为空,且至少>0
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        return service.selectStudentByPage(param);
    }

    /**
     * 插入学生
     * @param student 学生
     * @return 是否成功
     */
    @Log
    @PostMapping
    public ResultUtil insertStudent(@Validated@RequestBody InsertStudentParam student) {
        int inserted = service.insertStudent(student);
        return inserted == 0 ? ResultUtil.fail("插入失败") : ResultUtil.success("插入成功");
    }

    /**
     * 根据id查询学生
     * @param id id
     * @return 结果
     */
    @GetMapping("/{id}")
    public ResultUtil selectStudentById(@PathVariable("id") Integer id) {
        SelectStudentDetailDTO student = service.selectStudentById(id);
        return ResultUtil.success(student);
    }

    /**
     * 修改学生信息
     * @return 是否成功
     */
    @Log
    @PutMapping
    public ResultUtil updateStudent(@RequestBody UpdateStudentParam student) {
        int updated = service.updateStudent(student);
        return updated == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success("修改成功");
    }

    /**
     * 添加违纪信息
     * @param id 学生id
     * @param score 分数违纪
     * @return 结果
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public ResultUtil updateStudentViolation(@PathVariable("id") Integer id,@PathVariable("score")Integer score) {
        UpdateVioStudentParam param = new UpdateVioStudentParam();
        param.setId(id);
        param.setViolationScore(score);
        int i = service.updateStudentViolation(param);
        return ResultUtil.success(i);
    }

    /**
     * 批量删除学生
     * @param ids 学生id列表
     * @return 结果
     */
    @Log
    @DeleteMapping("/{ids}")
    public ResultUtil deleteStudentById(@PathVariable("ids") List<Integer> ids) {
        int deleted = service.deleteStudentByIds(ids);
        return deleted == 0 ? ResultUtil.fail("删除失败") : ResultUtil.success("删除成功");
    }

}
